import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useRef, forwardRef, useImperativeHandle, } from "react";
import { DevPopup } from "~/public/cpas-ui";
import { SelectBox, TextBox, TreeList } from "devextreme-react";
/*import AntTabs, {
  HocVerticalLeftStyleTab,
} from "../../components-ui/ant-components/AntTab";*/
import { getReportItemsData } from "./DraftApi";
import { Column } from "devextreme-react/tree-list";
import "./_style.less";
import { DevToolBar } from "~/public/cpas-ui";
import { Item } from "devextreme-react/toolbar";
import { CpasIcon } from "~/public/cpas-ui";
import notify from "devextreme/ui/notify";
import { fixTreeData } from "./met";
/**
 * ## 基于本地数据库，获取数据本地数据进行功能演示。
 * ~~~js
 * import ReportPopup  from 'sright-ui'
 * ~~~
 */
export const ReportPopup = forwardRef((props, ref) => {
    const Divstyle = {
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
    };
    const Titlestyle = { marginRight: "8px" };
    const [visible, setVisible] = useState(false);
    const [tabsData, setTabsData] = useState([]);
    const [SubjectSDBData, setSubjectSDBData] = useState([]);
    const [value, setValue] = useState("全部");
    const [values, setValues] = useState("");
    const [SubjectSDBDatas, setSubjectSDBDatas] = useState([]);
    const [selectMode, setSelectMode] = useState("");
    const diaParams = useRef();
    const diaParamsData = useRef();
    const TreeListRef = useRef();
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: async (params) => {
            diaParams.current = params;
            setVisible(true);
            setSelectMode(diaParams.current?.selectMode);
            await ReportItemsData();
        },
    }));
    // useEffect(() => {
    //   if (diaParams.current?.dbname) {
    //     ReportItemsData();
    //   }
    // }, [
    //   diaParams.current?.dbname,
    //   diaParams.current?.Data_ID,
    //   diaParams.current?.ownerReport,
    // ]);
    // 获取表格数据
    async function ReportItemsData() {
        const res = await getReportItemsData({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            ownerReport: diaParams.current?.ownerReport,
        });
        const KMLXData = res.filter((item) => {
            if (item.sumLevel === 0) {
                return item;
            }
            else {
                return "";
            }
        });
        // 因上级 科目删除导致二级不可显示
        const TreeData = fixTreeData(KMLXData, "code", "pCode");
        console.log(TreeData);
        setSubjectSDBData(TreeData);
        setSubjectSDBDatas(TreeData);
    }
    const onCellClick = (r) => {
        if (selectMode === "0") {
            diaParamsData.current = r.data;
        }
        else {
            const v = TreeListRef.current.instance.getSelectedRowsData("all");
            diaParamsData.current = v;
        }
    };
    const SelectBoxData = [
        {
            ID: "0",
            Name: "全部",
        },
        {
            ID: "1",
            Name: "资产类",
        },
        {
            ID: "2",
            Name: "负债类",
        },
        {
            ID: "3",
            Name: "所有者权益类",
        },
        {
            ID: "3",
            Name: "损益类",
        },
    ];
    const onselectionchanges = (e) => {
        setValue(e.selectedItem.Name);
        if (e.selectedItem.Name === "全部") {
            setSubjectSDBData(SubjectSDBDatas);
        }
        else {
            const FilterData = SubjectSDBDatas.filter((item) => {
                return item.type && item.type.includes(e.selectedItem.Name);
            });
            setSubjectSDBData(FilterData);
        }
    };
    // 模糊搜索
    const inputSearch = (content) => {
        TreeListRef.current.instance.searchByText(content || "");
    };
    // const throttleSearch = throttle(inputSearch, 100);
    const getInputValue = (e) => {
        const { event } = e;
        inputSearch(event.target.value);
        setValues(event.target.value);
    };
    return (_jsx("div", { children: _jsx(DevPopup.Logic, { ref: ref, width: 900, height: 700, visible: visible, title: diaParams.current?.title, onOK: () => {
                if (diaParams.current?.okCallback) {
                    if (diaParamsData.current == undefined) {
                        notify("请选择您要选择的科目", "warning", 2000);
                    }
                    else {
                        setVisible(false);
                        setValues("");
                        setValue("全部");
                        diaParams.current?.okCallback(diaParamsData.current);
                        TreeListRef.current.instance.deselectAll();
                        TreeListRef.current.instance.searchByText();
                    }
                }
            }, onCancel: () => {
                setVisible(false);
                setValues("");
                setValue("全部");
                TreeListRef.current.instance.deselectAll();
                TreeListRef.current.instance.searchByText();
            }, onHiding: () => {
                setVisible(false);
                setValues("");
                setValue("全部");
                TreeListRef.current.instance.deselectAll();
                TreeListRef.current.instance.searchByText();
            }, children: () => {
                return (_jsxs("div", { style: { width: "100%", height: "96%" }, children: [_jsxs(DevToolBar.Default, { className: "ItemStyle", children: [_jsx(Item, { location: "before", children: _jsxs("div", { style: {
                                            display: "flex",
                                            alignItems: "center",
                                            justifyContent: "center",
                                        }, children: [_jsx("div", { style: Titlestyle, children: "\u7C7B\u578B\uFF1A" }), _jsx(SelectBox, { dataSource: SelectBoxData, value: value, displayExpr: "Name", valueExpr: "Name", onSelectionChanged: onselectionchanges, width: 170 })] }) }), _jsx(Item, { location: "after", children: _jsxs("div", { style: {
                                            display: "flex",
                                            alignItems: "center",
                                            justifyContent: "center",
                                            marginLeft: "50px",
                                        }, children: [_jsx("span", { style: { marginRight: "15px" }, children: _jsx(TextBox, { onInput: (e) => getInputValue(e), value: values, width: 200, className: "TextBoxButtons_Style", buttons: [
                                                        {
                                                            location: "before",
                                                            name: "search",
                                                            options: {
                                                                icon: "iconfont icon-search",
                                                                disabled: true,
                                                            },
                                                        },
                                                    ] }) }), _jsx("span", { className: "refresh_Style", children: _jsx(CpasIcon, { fontSize: 15, type: "icon-refresh", onClick: () => {
                                                        ReportItemsData();
                                                        setValue("全部");
                                                        TreeListRef.current.instance.deselectAll();
                                                    } }) })] }) })] }), _jsxs(TreeList, { selection: {
                                recursive: true,
                                mode: selectMode === "0" ? "single" : "multiple",
                            }, className: "treeListReportClass", ref: TreeListRef, dataSource: SubjectSDBData, allowColumnResizing: true, columnAutoWidth: true, showRowLines: true, showBorders: true, columnResizingMode: "widget", columnMinWidth: 100, keyExpr: "code", parentIdExpr: "pCode", id: "GUID", height: "96%", onCellClick: onCellClick, 
                            // 显示斑马线
                            rowAlternationEnabled: true, children: [_jsx(Column, { dataField: "itemName", caption: "\u62A5\u8868\u9879\u76EE\u540D\u79F0", alignment: "left" }), _jsx(Column, { dataField: "type", caption: "\u7C7B\u578B", alignment: "left", width: 90 }), _jsx(Column, { dataField: "FX", caption: "\u65B9\u5411", width: 80, alignment: "left" })] })] }));
            } }) }));
});
export default ReportPopup;
